package org.pzy.java.hutool;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @Author: DRevelation
 * @Date: 2022/3/28 17:09
 */
public class HttpTest {
//    public static void main(String[] args) throws InterruptedException {
//        String ids = "16056, 16057, 16060, 16062";
//        String[] strArr = ids.split(",");
//        for (String str : strArr) {
//            String url = "https://gl0.ahtv.cn/api/product/cmsContentVideo/offline?id=" + str;
//            String body = HttpRequest.post(url)
//                    .header("token", "c11012ad3ab84e3da62f679e0a459383")
//                    .execute().body();
//            System.out.println(body);
//            Thread.sleep(300);
//        }
//    }

    public static final String liveUrl = "http://zbbf-hw.ahtv.cn//live/api/index/searchLive";

    public static void main(String[] args) {
        String month = "2023-01";
        DateTime beginOfMonth;
        DateTime endOfMonth;
        if (StrUtil.isNotBlank(month)) {
            DateTime dateTime = new DateTime(month, "yyyy-MM");
            beginOfMonth = DateUtil.beginOfMonth(dateTime);
            endOfMonth = DateUtil.endOfMonth(dateTime);
        } else {
            beginOfMonth = DateUtil.beginOfMonth(DateUtil.lastMonth());
            endOfMonth = DateUtil.endOfMonth(DateUtil.lastMonth());
        }
        String liveArgs = "&status=3&between_time=" + beginOfMonth + " ~ " + endOfMonth;
        System.out.println("liveArgs=" + liveArgs);

        String result = HttpRequest.get(liveUrl + "?page=1" + liveArgs)
                .header("domain", "tvzb-hw.ahtv.cn")
                .execute().body();

        System.out.println("result=" + result);
        JSONObject object = JSON.parseObject(result);
        JSONObject data = object.getJSONObject("data");
        JSONArray dataArray = data.getJSONArray("data");
        executeData(dataArray, month);

        Integer lastPage = data.getInteger("last_page");
        for (int i = 2; i <= lastPage; i++) {
            String result2 = HttpRequest.get(liveUrl + "?page=" + i + liveArgs)
                    .header("domain", "tvzb-hw.ahtv.cn")
                    .execute().body();
            JSONObject object2 = JSON.parseObject(result2);
            JSONObject data2 = object2.getJSONObject("data");
            JSONArray dataArray2 = data2.getJSONArray("data");
            executeData(dataArray2, month);
        }
    }

    private static void executeData(JSONArray dataArray, String month) {
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            System.out.println(jsonObject);
        }
    }

}
